package com.pumpkin.web.busi.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * (description)
 *
 * @author jinlong.liao on 2016/6/30
 */
public abstract class BaseController {

    protected Logger LOG = LoggerFactory.getLogger(getClass());

    /**
     * 用户跳转html页面
     * 此方法不考虑权限控制
     *
     * @param folder   路径
     * @param htmlName HTML名称(不加后缀)
     *
     * @return 指定HTML页面
     */
    @RequestMapping("/base/{folder}/{htmlName}")
    public String redirectHtml(@PathVariable String folder, @PathVariable String htmlName) {
        return folder + "/" + htmlName;
    }

    @RequestMapping("/base/{htmlName}")
    public String redirectHtml(@PathVariable String htmlName) {
        return htmlName;
    }

    /**
     * 初始化数据绑定
     * 1. 将字段中Date类型转换为String类型
     */
    @InitBinder
    public void initBinder(ServletRequestDataBinder binder) {
        /**
         * 自动转换日期类型的字段格式
         * 注意:页面传过来的字符串格式必须与下面制定的日期格式一致
         */
        binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true));
    }
}
